﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using HMIS.MVC.ViewModel;
using HMIS.MVC.CommonSupport;
using HMIS.FrameWork;
using HMIS.Infrastructure.Core.Authority;
using HMIS.Infrastructure.Facade.Authority;
using HMIS.FrameWork.Cache;
using System.ComponentModel;
using Iesi.Collections.Generic;

namespace HMIS.MVC.Controllers
{
    public class InfraUserRoleDepController : BaseController
    {
        private readonly IHospUserFacade HospUserFacade;
        private readonly IDepartmentFacade DepartmentFacade;
        private readonly IRoleFacade RoleFacade;

		private readonly IUserRoleDepFacade UserRoleDepFacade;

		public InfraUserRoleDepController(IUserRoleDepFacade UserRoleDepFacade, IHospUserFacade HospUserFacade, IDepartmentFacade DepartmentFacade, IRoleFacade RoleFacade)
        {
            this.HospUserFacade = HospUserFacade;
            this.DepartmentFacade = DepartmentFacade;
            this.RoleFacade = RoleFacade;
			this.UserRoleDepFacade = UserRoleDepFacade;
        }

        [Description("访问用户管理页面")]
        [ViewPage]
        public ActionResult HospUser()
        {
            return View();
        }

        [ExtResult]
        [Description("删除用户角色")]
        public ActionResult Delete()
        {
			UserRoleDepFacade.PhysicsDelete(Request["Id"]);
            return this.JsonFormat((new ExtResult { success = true }));
        }

		[ExtResult]
		[Description("添加用户角色")]
		public ActionResult SaveUserDepRole()
		{
			UserRoleDep entity;
			entity = new UserRoleDep
			{
				Person = HospUserFacade.Load(Request["UserID"]),
				Role = RoleFacade.Load(Request["RoleID"]),
				Department = DepartmentFacade.Load(Request["DepartmentID"])
			};
			UserRoleDepFacade.Save(entity);
			return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
		}
		
		[Description("按机构和角色获取用户列表")]
		public ActionResult QueryUserListByCode()
		{
			long total;
			var data = UserRoleDepFacade.QueryUserListByCode(
				Request["query"],
				Request["userCode"],
				Request["roleCode"],
				Request["deptCode"],
				int.Parse(Request["start"] == null ? "0" : Request["start"]),
				int.Parse(Request["limit"] == null ? "0" : Request["limit"]),
				Request["sort"],
				Request["dir"],
				this.DesDataFilter(Request["filter"]),
				out total);
			return this.JsonFormat(
				new ExtGirdData<object[]> { total = total, data = data },
				//new ExtGirdData<UserRoleDep> { total = total, data = data },
				new string[] { "CustomType" });
		}
    }
}
